package com.android.PopFax;


import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.R;

public class Bean extends Activity implements Runnable,View.OnClickListener{


	EditText userName;
	EditText password;
	Button btnSignIn;
	private ProgressDialog pd ;
	String finalResult="";
	View view ;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main); 
		userName=(EditText)findViewById(R.id.userName);
		password=(EditText)findViewById(R.id.password);
		btnSignIn=(Button)findViewById(R.id.btnSighIn);

		btnSignIn.setOnClickListener(this);
	}

	
	public void onClick(View v)
	{
		view=v;
		pd = ProgressDialog.show(this, "Logging In..", "Authenticating..", true,false);
		Thread thread = new Thread(this);
		thread.start();

	}

	
	public void run() {
		try {
			HashMap<String,String> data= new HashMap<String, String>();
			data.put("action","authentication");
			//data.put("username",userName.getText().toString());
			data.put("username","mtcsindia@gmail.com");
			//data.put("password",password.getText().toString());
			data.put("password","5znavqps");

//			InputStream toastData= (doSubmit("https://api.popfax.com/api-server.php", data));

			/* Get a SAXParser from the SAXPArserFactory. */ 
//			SAXParserFactory spf = SAXParserFactory.newInstance(); 
//			SAXParser sp = spf.newSAXParser(); 

			/* Get the XMLReader of the SAXParser we created. */ 
//			XMLReader xr = sp.getXMLReader(); 
			/* Create a new ContentHandler and apply it to the XML-Reader*/ 
//			ExampleHandler myExampleHandler = new ExampleHandler(); 
//			xr.setContentHandler(myExampleHandler); 

			/* Parse the xml-data from our URL. */ 
//			xr.parse(new InputSource(toastData)); 
			/* Parsing has finished. */ 

			/* Our ExampleHandler now provides the parsed data to us. */ 
//			ParsedExampleDataSet parsedExampleDataSet = 
//				myExampleHandler.getParsedData(); 

			/* Set the result to be displayed in our GUI. */ 


	
//			if((parsedExampleDataSet.intReturn()==0))
//			{
				finalResult="Logged In Successfully";

				//Intent mIntent = new Intent(v.getContext(),loginBox.class);
				//startActivity(mIntent);


//			}else if ((parsedExampleDataSet.intReturn()==107))
	//		{
		//		finalResult="Logged In Successfully";
				//	finalResult="UserName/Password Invalid";
				//    	Toast.makeText(getApplicationContext(),finalResult, Toast.LENGTH_SHORT).show();
		//	}else
			//{
				//finalResult="Logged In Successfully";
			//	finalResult="Other Error";
				//    	Toast.makeText(getApplicationContext(),finalResult, Toast.LENGTH_SHORT).show();
			//}


		}catch(Exception e)
		{
			e.printStackTrace();
		}


		handler.sendEmptyMessage(0);


	}




	private Handler handler = new Handler() {

		public void handleMessage(Message msg) {
			pd.dismiss();
			Toast.makeText(getApplicationContext(),finalResult, Toast.LENGTH_SHORT).show();
			if(finalResult.equals("Logged In Successfully"))
			{
				Intent mIntent = new Intent(view.getContext(),com.android.tabtestpack.HelloTabWidget.class);
				startActivity(mIntent);
			}


		}
	};



	public InputStream doSubmit(String url, HashMap <String,String> data)throws Exception{


		try{

			URL siteUrl= new URL(url);
			HttpURLConnection conn= (HttpURLConnection)siteUrl.openConnection();
			conn.setRequestMethod("POST");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			DataOutputStream dataOutputStream=new DataOutputStream(conn.getOutputStream());

			Set keys=data.keySet();
			Iterator iter= keys.iterator();
			String content="";
			for(int m=0;iter.hasNext();m++){
				Object key=iter.next();
				if(m!=0)
				{
					content +="&";
				}
				content +=key+"="+URLEncoder.encode(data.get(key),"UTF-8");
			}


			dataOutputStream.writeBytes(content);
			dataOutputStream.flush();
			dataOutputStream.close();

			/*
	BufferedReader bufferedReader= new BufferedReader(new InputStreamReader (conn.getInputStream()));
	String line="";
	String retLine = "";
	while ((line=bufferedReader.readLine())!= null){
		System.out.println(line);
		retLine = retLine + line;
	}

	bufferedReader.close();*/
			return conn.getInputStream();

		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

}





















